package com.zoe.onelink.auth.autoconfigure;

import com.zoe.onelink.auth.interceptor.OnelinkTokenInterceptor;
import com.zoe.onelink.auth.property.OnelinkAuthProperties;
import com.zoe.onelink.auth.token.authenticate.OnelinkTokenAuthenticate;
import com.zoe.onelink.common.constant.WebConstant;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.MessageSource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-05-12
 */
@RequiredArgsConstructor
@AutoConfigureAfter(OnelinkAuthAutoConfiguration.class)
public class OnelinkAuthWebMvcAutoConfiguration implements WebMvcConfigurer {

    private final OnelinkAuthProperties onelinkAuthProperties;
    private final OnelinkTokenAuthenticate onelinkTokenAuthenticate;
    private final MessageSource messageSource;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        OnelinkTokenInterceptor onelinkTokenInterceptor = new OnelinkTokenInterceptor(
                this.onelinkAuthProperties, this.onelinkTokenAuthenticate, this.messageSource);
        registry.addInterceptor(onelinkTokenInterceptor).order(100)
                .addPathPatterns("/**")
                .excludePathPatterns("/inner/**")
                .excludePathPatterns("/admin/**")
                .excludePathPatterns("/open/**")
                .excludePathPatterns(new ArrayList<>(this.onelinkAuthProperties.getPermitUrls()))
                .excludePathPatterns(WebConstant.INSENSITIVE_URLS);
    }

}
